package com.zhku.xxl.dao.daoImp;

import com.zhku.xxl.dao.GraduateDao;
import com.zhku.xxl.entity.Graduate;
import com.zhku.xxl.entity.vo.GraduateVo;
import jdbc.BasicDao;
import jdbc.GenericDao;

import java.util.ArrayList;
import java.util.List;

public class GraduateDaoImp extends GenericDao<Graduate> implements GraduateDao {


    @Override
    public List<GraduateVo> graduateList(String graduationDate, String major, String majorClass, String name) {
        StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM Graduate WHERE 1=1");
        List<Object> params = new ArrayList<>();
        if (graduationDate != null && !graduationDate.isEmpty()) {
            sqlBuilder.append(" AND  graduationDate > ?");
            params.add(graduationDate);
        }
        if (major != null && !major.isEmpty()) {
            sqlBuilder.append(" AND departMajor LIKE CONCAT('%', ?, '%')");
            params.add(major);
        }
        if (majorClass != null && !majorClass.isEmpty()) {
            sqlBuilder.append(" AND graduateClass LIKE CONCAT('%', ?, '%')");
            params.add(majorClass);
        }
        if (name != null && !name.isEmpty()) {
            sqlBuilder.append(" AND name LIKE CONCAT('%', ?, '%')");
            params.add(name);
        }

        String sql = sqlBuilder.toString();
        return queryMultiple(sql, super.createRowMapper(GraduateVo.class), params.toArray());
    }

    @Override
    public int deleteForName(String name) {
        String sql = "DELETE FROM Graduate"
                     + " WHERE name = ?";

        return super.update(sql,name);
    }

    @Override
    public GraduateVo queryByStudentId(String id) {
        String sql = "SELECT * FROM Graduate"
                + " WHERE studentId = ?";
        return BasicDao.querySingle(sql,createRowMapper(GraduateVo.class),id);
    }

    @Override
    public Graduate queryById(int id) {
        String sql = "SELECT * FROM Graduate"
                + " WHERE Id = ?";
        return BasicDao.querySingle(sql,createRowMapper(Graduate.class),id);
    }
}
